<?php

namespace app\common\logic;

use think\Model;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 逻辑层模型
 */
class Memberevaluate extends Model
{

    public function evaluateListDity($goods_eval_list)
    {
        foreach ($goods_eval_list as $key => $value) {
            $goods_eval_list[$key]['member_avatar'] = get_member_avatar_for_id($value['geval_frommemberid']);
        }
        return $goods_eval_list;
    }

    /* 查询订单信息 */

    public function validation($order_id, $member_id)
    {
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $member_id;

        //获取订单信息
        $order_info = model('order')->getOrderInfo($condition);
        if (empty($order_info)) {
            $info = array('state' => '0', 'msg' => '订单出错');
        }
        $order_info['evaluate_able'] = model('order')->getOrderOperateState('evaluation', $order_info);
        if (empty($order_info) || !$order_info['evaluate_able']) {
            $info = array('state' => '0', 'msg' => '评价订单不存在');
        }
        //查询店铺信息
        $store_info = model('store')->getStoreInfoByID($order_info['store_id']);
        if (empty($store_info)) {
            $info = array('state' => '0', 'msg' => '店铺不存在');
        }

        //获取订单商品
        $order_goods = model('order')->getOrdergoodsList($condition);
        if (empty($order_goods)) {
            $info = array('state' => '0', 'msg' => '订单商品不存在');
        }
        foreach ($order_goods as $key => $value) {
            $order_goods[$key]['goods_image_url'] = goods_cthumb($value['goods_image']);
        }


        $info['data'] = array('order_goods' => $order_goods, 'store_info' => $store_info, 'order_info' => $order_info);
        return $info;
    }

    /* 保存订单评价信息 */
    public function saveorderevaluate($order_info, $store_info, $order_goods, $member_id, $member_name, $evaluation_type, $goods_id, $rec_id, $order_id, $user_info)
    {
        $evaluate_goods_array = array();

        //商品评价： 评价内容  是否匿名  图片
        $geval_content = input('post.comment');
        $anony = input('post.anony');
        $geval_image = input('post.geval_image/a');
        //将图片转为json存起来
        $pic_info = json_encode($geval_image);


        //根据evaluation_type 来区分是类型的评价 1--商城 2--服务端 3--项目定制
        if ($evaluation_type == 1) {
            //商城评价
            $score = input('post.score');
            //获取订单详情表的数据
            $goods_info = db('ordergoods')->where(['goods_id' => $goods_id])->find();
            $evaluate_goods_array = [
                'geval_orderid' => $order_info['order_id'],
                'geval_orderno' => $order_info['order_sn'],
                'geval_ordergoodsid' => $goods_info['rec_id'],
                'geval_goodsid' => $goods_id,
                'geval_goodsname' => $goods_info['goods_name'],
                'geval_goodsprice' => $goods_info['goods_price'],
                'geval_goodsimage' => $goods_info['goods_image'],
                'geval_scores' => $score,
                'geval_content' => $geval_content,
                'anony' => $anony,
                'geval_addtime' => time(),
                'geval_storeid' => $store_info['store_id'],
                'geval_storename' => $store_info['store_name'],
                'geval_frommemberid' => $member_id,
                'geval_frommembername' => $user_info['member_nickname'],
                'evaluation_type' => $evaluation_type,
                'geval_image' => $pic_info
            ];

            //修改评价状态
            db('order')->where(['order_id' => $order_id])->update(['evaluation_state' => 1]);
            db('ordergoods')->where(['rec_id' => $rec_id])->update(['return_status' => 5]);
            $state = db('evaluategoods')->insertGetId($evaluate_goods_array);

            //获取商品详情
            $goods = db('goods')->where(['goods_id' => $goods_id])->find();

            //评价完之后加商品对应的积分
            $member_info = db('member')->where(['member_id' => $member_id])->find();
            if (!empty($goods['score'])) {
                //积分记录表
                $points_info  = [
                    'pl_memberid' => $member_id,
                    'pl_membername' => $member_name,
                    'pl_points' => $goods['score'],
                    'pl_addtime' => time(),
                    'pl_desc' => '评论商品'. $order_info['order_sn'],
                    'pl_stage' => 'evaluate'
                ];
                db('pointslog')->insertGetId($points_info);
                $update_points = $member_info['member_points'] + $goods['score'];
                db('member')->where(['member_id' => $member_id])->update(['member_points' => $update_points]);
            }
        } elseif ($evaluation_type == 2) {
            //服务单评价
            $geval_attitude = input('post.geval_attitude');
            $geval_technical = input('post.geval_technical');
            $geval_quality = input('post.geval_quality');

            //获取服务单 主单详情
            $order_info = db('s_order')->where(['order_id' => $order_id])->find();

            if ($order_info['evaluation_state'] == 1) {
                return json_fail_return('当前订单已评价，无需重复');
            }
            //获取商品服务单
            $goods_info = db('s_order_detail')->where(['order_id' => $order_id])->find();

            $evaluate_goods_array = [
                'geval_orderid' => $order_id,
                'geval_orderno' => $order_info['order_sn'],
                'geval_ordergoodsid' => $goods_info['id'],
                'geval_goodsid' => $goods_info['goods_id'],
                'geval_goodsname' => $goods_info['goods_name'],
                'geval_content' => $geval_content,
                'anony' => $anony,
                'geval_addtime' => time(),
                'geval_frommemberid' => $member_id,
                'geval_frommembername' => $user_info['member_nickname'],
                'evaluation_type' => $evaluation_type,
                'geval_image' => $pic_info,
                'geval_attitude' => $geval_attitude,
                'geval_technical' => $geval_technical,
                'geval_quality' => $geval_quality
            ];

            //修改评价状态
            db('s_order')->where(['order_id' => $order_id])->update(['evaluation_state' => 1]);
            $state = db('evaluategoods')->insertGetId($evaluate_goods_array);
        } else {
            //项目定制评价
            $score = input('post.score');

            #获取项目定制订单信息
            $project_info = db('orderproject')->where(['order_id' => $order_id])->find();

            $evaluate_goods_array = [
                'geval_orderid' => $order_info['order_id'],
                'geval_orderno' => $order_info['order_sn'],
                'geval_goodsname' => $project_info['project_name'],
                'geval_scores' => $score,
                'geval_content' => $geval_content,
                'anony' => $anony,
                'geval_addtime' => time(),
                'geval_storeid' => 6,
                'geval_storename' => '',
                'geval_frommemberid' => $member_id,
                'geval_frommembername' => $user_info['member_nickname'],
                'evaluation_type' => $evaluation_type,
                'geval_image' => $pic_info,
                'geval_projectid' => $project_info['project_id']
            ];

            //修改评价状态
            db('order')->where(['order_id' => $order_id])->update(['evaluation_state' => 1]);
            db('orderproject')->where(['order_id' => $order_id])->update(['evaluation_state' => 1]);

            $state = db('evaluategoods')->insertGetId($evaluate_goods_array);
        }

        model('order')->editOrdercommon(array('evaluation_time' => TIMESTAMP), array('order_id' => $order_info['order_id']));
        if ($state) {
            $data = array();
            $data['order_id'] = $order_info['order_id'];
            $data['log_role'] = 'buyer';
            $data['log_msg'] = '评价了交易';
            $data['log_user'] = $member_name;
            model('order')->addOrderlog($data);
            $res = true;
        } else {
            $res = false;
        }

        return $res;
    }

    public function validationVr($order_id, $member_id)
    {
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $member_id;
        //获取订单信息
        $order_info = model('vrorder')->getVrorderInfo($condition);
        if (empty($order_info)) {
            $info = array(
                'state' => '0', 'msg' => '没有权限'
            );
        }
        //订单为'已收货'状态，并且未评论
        $order_info['evaluate_able'] = model('vrorder')->getVrorderOperateState('evaluation', $order_info);
        if (!$order_info['evaluate_able']) {
            $info = array(
                'state' => '0', 'msg' => '订单已评价'
            );
        }
        //查询店铺信息
        $store_info = model('store')->getStoreInfoByID($order_info['store_id']);
        if (empty($store_info)) {
            $info = array(
                'state' => '0', 'msg' => '评价店铺不存在'
            );
        }
        //单个商品
        $order_info['goods_image_url'] = goods_cthumb($order_info['goods_image']);
        $info['data'] = array('order_info' => $order_info, 'store_info' => $store_info);
        return $info;
    }

    public function saveVr($order_info, $store_info, $order_goods, $member_id, $member_name)
    {
        $evaluate_goods_array = array();
        $goodsid_array = array();
        $vrorder_model = model('vrorder');
        $evaluategoods_model = model('evaluategoods');
        $goods_array = input('post.goods/a'); #获取数组
        foreach ($order_goods as $value) {
            //如果未评分，默认为5分
            $evaluate_score = intval($goods_array[$value['goods_id']]['score']);
            if ($evaluate_score <= 0 || $evaluate_score > 5) {
                $evaluate_score = 5;
            }
            //默认评语
            $evaluate_comment = $goods_array[$value['goods_id']]['comment'];
            if (empty($evaluate_comment)) {
                $evaluate_comment = '不错哦';
            }

            $evaluate_goods_info = array();
            $evaluate_goods_info['geval_orderid'] = $order_info['order_id'];
            $evaluate_goods_info['geval_orderno'] = $order_info['order_sn'];
            $evaluate_goods_info['geval_ordergoodsid'] = $order_info['order_id'];
            $evaluate_goods_info['geval_goodsid'] = $value['goods_id'];
            $evaluate_goods_info['geval_goodsname'] = $value['goods_name'];
            $evaluate_goods_info['geval_goodsprice'] = $value['goods_price'];
            $evaluate_goods_info['geval_goodsimage'] = $value['goods_image'];
            $evaluate_goods_info['geval_scores'] = $evaluate_score;
            $evaluate_goods_info['geval_content'] = $evaluate_comment;
            $evaluate_goods_info['geval_isanonymous'] = input('post.anony') ? 1 : 0;
            $evaluate_goods_info['geval_addtime'] = TIMESTAMP;
            $evaluate_goods_info['geval_storeid'] = $store_info['store_id'];
            $evaluate_goods_info['geval_storename'] = $store_info['store_name'];
            $evaluate_goods_info['geval_frommemberid'] = $member_id;
            $evaluate_goods_info['geval_frommembername'] = $member_name;

            $evaluate_goods_array[] = $evaluate_goods_info;

            $goodsid_array[] = $value['goods_id'];
        }
        $evaluategoods_model->addEvaluategoodsArray($evaluate_goods_array, $goodsid_array);

        //更新订单信息并记录订单日志
        $state = $vrorder_model->editVrorder(array('evaluation_state' => 1, 'evaluation_time' => TIMESTAMP), array('order_id' => $order_info['order_id']));
        //添加会员积分
        if (config('points_isuse') == 1) {
            $points_model = model('points');
            $points_model->savePointslog('comments', array('pl_memberid' => $member_id, 'pl_membername' => $member_name));
        }
        //添加会员经验值
        model('exppoints')->saveExppointslog('comments', array('explog_memberid' => $member_id, 'explog_membername' => $member_name));
        return $state;
    }
}
